﻿var jsObjP61 = null;
var strFilterP61 = null;
var curDataGridP61 = null;
function fileclick(id, e)
{
    var txt = $get(otP61.tNhomDoiTuongIdP61CID);
    var fileUp1 =  $get("<% =fileUp1.ClientID %>");
    fileUp1.click();
}
function PrintP61(Id)
{
}
function calculateTTTongTien(textId)
{
	var txtbox = $get(textId);
	var txtNoLaiP61 = $get("<% =tNoLaiP61.ClientID %>");
	var txtThucThuP61 = $get("<% =tThucThuP61.ClientID %>");
	if( (txtThucThuP61.value!=null) && (txtThucThuP61.value.length > 0))
		if( (txtbox.value !=null) && (txtbox.value.length > 0) )
		{
		txtNoLaiP61.value=parseFloat(gcRev(txtbox.value, ','))-parseFloat(gcRev(txtThucThuP61.value, ','));
		txtNoLaiP61.value = parseFloat(txtNoLaiP61.value).format('n0');
	}
	txtbox.value = gcNum(txtbox.value, ',', '.');
	txtNoLaiP61.value = gcNum(txtNoLaiP61.value, ',', '.');
}
function calculateTTThucThu(textId)
{
	var txtbox = $get(textId);
	var txtNoLaiP61 = $get("<% =tNoLaiP61.ClientID %>");
	var txtTongTienP61 = $get("<% =tTongTienP61.ClientID %>");
	if( (txtTongTienP61.value!=null) && (txtTongTienP61.value.length > 0))
		if( (txtbox.value !=null) && (txtbox.value.length > 0) )
		{
		txtNoLaiP61.value=parseFloat(gcRev(txtTongTienP61.value, ','))-parseFloat(gcRev(txtbox.value, ','));
		txtNoLaiP61.value = parseFloat(txtNoLaiP61.value).format('n0');
	}
	txtbox.value = gcNum(txtbox.value, ',', '.');
	txtNoLaiP61.value = gcNum(txtNoLaiP61.value, ',', '.');
}
function DelP61(Id)
{
    if(Id)
    {
       if(confirm('Bạn thực sự muốn xóa dữ liệu?'))
       {
          var pagesize = 10;
          var sortExp = '';
          var tableView = $find(otP61.rg).get_masterTableView();
          var currentPageIndex = tableView.get_currentPageIndex();
          var page = pagesize*currentPageIndex;
          WSMailisa_PhieuThuTienMat.DeleteData(parseInt(Id), page.toString(), pagesize, sortExp, strFilterP61, updateGridP61, deleteFaildP61);
			WSMailisa_PhieuThuTienMat.GetCount(strFilterP61, updateVirtualItemCountP61);
       }
    }
}
function AddUpdateP61()
{
    var pagesize = 10;
    var sortExp = '';
    jsObjP61 = getValuesP61();
    WSMailisa_PhieuThuTienMat.ValidForm(jsObjP61, 0, upStrP61, validFaildP61);
}
function upStrP61(str)
{
    $get(otP61.lErrorCID).innerHTML = str;
    if(str.length>0)
        return  false;
    var sortExp = '';
    var tableView = $find(otP61.rg).get_masterTableView();
    var pagesize = tableView.get_pageSize();
    var currentPageIndex = tableView.get_currentPageIndex();
    var page = pagesize*currentPageIndex;
    WSMailisa_PhieuThuTienMat.UpdateData(jsObjP61, page.toString(), pagesize, sortExp, strFilterP61, updateGridP61, updateFaildP61());
    $get(otP61.lErrorCID).innerHTML =FormatDiv('','Cập nhật dữ liệu thành công.');
}
function updateFaildP61()
{
}
function deleteFaildP61()
{
}
function validFaildP61()
{
		alert('Lỗi xãy ra khi chuyển đổi kiểu sai ở hàm getValue()!\r\n<strong>Chú ý kiểm tra kiểu của Khóa ngoại ( nếu lúc design không kéo liên kết) thì sai kiểu Text->int).\r\nGiá trị của khóa ngoại phải lấy từ TextBox.alt chứ không phải TextBox.value.</strong>');
}
function addFaildP61()
{
}
function adStrP61(str)
{
    $get(otP61.lErrorCID).innerHTML = str;
    if(str.length>0)
        return  false;
    var sortExp = '';
    $find('<%= Page.FindControl("gcLD").ClientID %>').show(otP61.rg);
    var tableView = $find(otP61.rg).get_masterTableView();
    var pagesize = tableView.get_pageSize();
    WSMailisa_PhieuThuTienMat.AddData(jsObjP61, pagesize*1, pagesize, sortExp, strFilterP61,  updateGridP61, addFaildP61);
    WSMailisa_PhieuThuTienMat.GetCount(strFilterP61, updateVirtualItemCountP61);
    $find('<%= Page.FindControl("gcLD").ClientID %>').hide(otP61.rg);
    $get(otP61.lErrorCID).innerHTML =FormatDiv('','Thêm mới dữ liệu thành công.');
}
function AddNewP61()
{
    var index = 1; 
    var pagesize = 10;
    var sortExp = '';
    jsObjP61 = getValuesP61();
    WSMailisa_PhieuThuTienMat.ValidForm(jsObjP61, 1, adStrP61, validFaildP61);
}
function setValuesP61()
{
    var tableView = $find(otP61.rg).get_masterTableView();
    var dtSource = tableView.get_dataSource();
    var pos = -1;
    for(i=0; i<dtSource.length; i++)
        if(dtSource[i].Id == curObjIdP61)
            pos = i;
    if(pos>=0)
    {
         jsObjP61 = dtSource[pos];
        $get(otP61.tTenP61CID).value = (jsObjP61.Ten==null)?'':jsObjP61.Ten;
        $get(otP61.tSoCTP61CID).value = (jsObjP61.SoCT==null)?'':jsObjP61.SoCT;
        $get(otP61.tNgayLapP61CID).value = (jsObjP61.NgayLap==null)?'':jsObjP61.NgayLap.format('dd/MM/yyyy');
        $get(otP61.tVuviecIdP61CID).alt = (jsObjP61.VuviecId==null)?'':jsObjP61.VuviecId;
        $get(otP61.tVuviecIdP61CID).value = (jsObjP61.VuviecId_Name4==null)?'':jsObjP61.VuviecId_Name4;
        $get(otP61.tNhomDoiTuongIdP61CID).alt = (jsObjP61.NhomDoiTuongId==null)?'':jsObjP61.NhomDoiTuongId;
        $get(otP61.tNhomDoiTuongIdP61CID).value = (jsObjP61.NhomDoiTuongId_Name5==null)?'':jsObjP61.NhomDoiTuongId_Name5;
        $get(otP61.tDoiTuongIdP61CID).alt = (jsObjP61.DoiTuongId==null)?'':jsObjP61.DoiTuongId;
        $get(otP61.tDoiTuongIdP61CID).value = (jsObjP61.DoiTuongId_Name6==null)?'':jsObjP61.DoiTuongId_Name6;
        $get(otP61.tTongTienP61CID).value = (jsObjP61.TongTien==null)?'':jsObjP61.TongTien.format('n0');
        $get(otP61.tThucThuP61CID).value = (jsObjP61.ThucThu==null)?'':jsObjP61.ThucThu.format('n0');
        $get(otP61.tNoLaiP61CID).value = (jsObjP61.NoLai==null)?'':jsObjP61.NoLai.format('n0');
        $get(otP61.tDienGiaiP61CID).value = (jsObjP61.DienGiai==null)?'':jsObjP61.DienGiai;
        $get(otP61.tNhanVienIdP61CID).alt = (jsObjP61.NhanVienId==null)?'':jsObjP61.NhanVienId;
        $get(otP61.tNhanVienIdP61CID).value = (jsObjP61.NhanVienId_HoTen11==null)?'':jsObjP61.NhanVienId_HoTen11;
        $get(otP61.tThanhToanP61CID).checked = (jsObjP61.ThanhToan==null)?false:jsObjP61.ThanhToan;
        $get(otP61.tisDatCocP61CID).checked = (jsObjP61.isDatCoc==null)?false:jsObjP61.isDatCoc;
        $get(otP61.tSoTienCocP61CID).value = (jsObjP61.SoTienCoc==null)?'':jsObjP61.SoTienCoc.format('n0');
        $get(otP61.tisTraNoP61CID).checked = (jsObjP61.isTraNo==null)?false:jsObjP61.isTraNo;
        $get(otP61.tSoTienNoP61CID).value = (jsObjP61.SoTienNo==null)?'':jsObjP61.SoTienNo.format('n0');
        $get(otP61.tisFinishedP61CID).checked = (jsObjP61.isFinished==null)?false:jsObjP61.isFinished;
        $get(otP61.tBranhIdP61CID).alt = (jsObjP61.BranhId==null)?'':jsObjP61.BranhId;
        $get(otP61.tBranhIdP61CID).value = (jsObjP61.BranhId_Name18==null)?'':jsObjP61.BranhId_Name18;
        $get(otP61.tKhuyenMaiP61CID).value = (jsObjP61.KhuyenMai==null)?'':jsObjP61.KhuyenMai.format('n0');
        $get(otP61.tDichVuDatCocIdP61CID).value = (jsObjP61.DichVuDatCocId==null)?'':jsObjP61.DichVuDatCocId.format('n0');
        $get(otP61.tBoPhanDatCocIdP61CID).value = (jsObjP61.BoPhanDatCocId==null)?'':jsObjP61.BoPhanDatCocId.format('n0');
        $get(otP61.tNgayHenP61CID).value = (jsObjP61.NgayHen==null)?'':jsObjP61.NgayHen.format('dd/MM/yyyy');
		  curObj = jsObjP61;
        getValuesP61();
    }
}
function getValuesP61()
{
    gcMailisa_PhieuThuTienMat.Id = curObjIdP61;
    var tmpValue;
    tmpValue = $get(otP61.tTenP61CID).value;
    gcMailisa_PhieuThuTienMat.Ten = (tmpValue=='')?' ':tmpValue;
    tmpValue = $get(otP61.tSoCTP61CID).value;
    gcMailisa_PhieuThuTienMat.SoCT = (tmpValue=='')?' ':tmpValue;
    tmpValue = $get(otP61.tNgayLapP61CID).value;
    gcMailisa_PhieuThuTienMat.NgayLap = (tmpValue=='')?null:gcDateC(tmpValue);
    tmpValue = $get(otP61.tVuviecIdP61CID).alt;
    gcMailisa_PhieuThuTienMat.VuviecId = (tmpValue=='')?null:tmpValue;
    tmpValue = trim($get(otP61.tVuviecIdP61CID).value);
    if(tmpValue=='') gcMailisa_PhieuThuTienMat.VuviecId = null;
    tmpValue = $get(otP61.tNhomDoiTuongIdP61CID).alt;
    gcMailisa_PhieuThuTienMat.NhomDoiTuongId = (tmpValue=='')?null:tmpValue;
    tmpValue = trim($get(otP61.tNhomDoiTuongIdP61CID).value);
    if(tmpValue=='') gcMailisa_PhieuThuTienMat.NhomDoiTuongId = null;
    tmpValue = $get(otP61.tDoiTuongIdP61CID).alt;
    gcMailisa_PhieuThuTienMat.DoiTuongId = (tmpValue=='')?null:tmpValue;
    tmpValue = trim($get(otP61.tDoiTuongIdP61CID).value);
    if(tmpValue=='') gcMailisa_PhieuThuTienMat.DoiTuongId = null;
    tmpValue = $get(otP61.tTongTienP61CID).value;
    gcMailisa_PhieuThuTienMat.TongTien = (tmpValue=='')?null:gcRev(tmpValue, ',');
    tmpValue = $get(otP61.tThucThuP61CID).value;
    gcMailisa_PhieuThuTienMat.ThucThu = (tmpValue=='')?null:gcRev(tmpValue, ',');
    tmpValue = $get(otP61.tNoLaiP61CID).value;
    gcMailisa_PhieuThuTienMat.NoLai = (tmpValue=='')?null:gcRev(tmpValue, ',');
    tmpValue = $get(otP61.tDienGiaiP61CID).value;
    gcMailisa_PhieuThuTienMat.DienGiai = (tmpValue=='')?' ':tmpValue;
    tmpValue = $get(otP61.tNhanVienIdP61CID).alt;
    gcMailisa_PhieuThuTienMat.NhanVienId = (tmpValue=='')?null:tmpValue;
    tmpValue = trim($get(otP61.tNhanVienIdP61CID).value);
    if(tmpValue=='') gcMailisa_PhieuThuTienMat.NhanVienId = null;
    tmpValue = $get(otP61.tThanhToanP61CID).value;
    tmpValue = $get(otP61.tThanhToanP61CID).checked;
    gcMailisa_PhieuThuTienMat.ThanhToan =  tmpValue;
    tmpValue = $get(otP61.tisDatCocP61CID).value;
    tmpValue = $get(otP61.tisDatCocP61CID).checked;
    gcMailisa_PhieuThuTienMat.isDatCoc =  tmpValue;
    tmpValue = $get(otP61.tSoTienCocP61CID).value;
    gcMailisa_PhieuThuTienMat.SoTienCoc = (tmpValue=='')?null:gcRev(tmpValue, ',');
    tmpValue = $get(otP61.tisTraNoP61CID).value;
    tmpValue = $get(otP61.tisTraNoP61CID).checked;
    gcMailisa_PhieuThuTienMat.isTraNo =  tmpValue;
    tmpValue = $get(otP61.tSoTienNoP61CID).value;
    gcMailisa_PhieuThuTienMat.SoTienNo = (tmpValue=='')?null:gcRev(tmpValue, ',');
    tmpValue = $get(otP61.tisFinishedP61CID).value;
    tmpValue = $get(otP61.tisFinishedP61CID).checked;
    gcMailisa_PhieuThuTienMat.isFinished =  tmpValue;
    tmpValue = $get(otP61.tBranhIdP61CID).alt;
    gcMailisa_PhieuThuTienMat.BranhId = (tmpValue=='')?null:tmpValue;
    tmpValue = trim($get(otP61.tBranhIdP61CID).value);
    if(tmpValue=='') gcMailisa_PhieuThuTienMat.BranhId = null;
    tmpValue = $get(otP61.tKhuyenMaiP61CID).value;
    gcMailisa_PhieuThuTienMat.KhuyenMai = (tmpValue=='')?null:gcRev(tmpValue, ',');
    tmpValue = $get(otP61.tDichVuDatCocIdP61CID).value;
    gcMailisa_PhieuThuTienMat.DichVuDatCocId = (tmpValue=='')?null:gcRev(tmpValue, ',');
    tmpValue = $get(otP61.tBoPhanDatCocIdP61CID).value;
    gcMailisa_PhieuThuTienMat.BoPhanDatCocId = (tmpValue=='')?null:gcRev(tmpValue, ',');
    tmpValue = $get(otP61.tNgayHenP61CID).value;
    gcMailisa_PhieuThuTienMat.NgayHen = (tmpValue=='')?null:gcDateC(tmpValue);
    curObj = gcMailisa_PhieuThuTienMat;
    return gcMailisa_PhieuThuTienMat;
}
function RgP61RowSelected(sender, args)
{
    curRowIndexP61 = args.get_gridDataItem().get_element().rowIndex;
    curObjIdP61 = args.get_gridDataItem().get_dataItem()["Id"];
    setValuesP61();
    if(typeof updateDetailChild == 'function')
	      updateDetailChild(jsObjP61);
}
function updateVirtualItemCountP61(result)
{
	curRg = otP61.rg;
  if(curRg)
  {
	   var tableView = $find(curRg).get_masterTableView();
	   tableView.set_virtualItemCount(result);
  }
}
function updateGridP61(result)
{
if(!result)
return;
	curRg = otP61.rg;
	if(curRg)
	{
	   curDataGridP61 = result;
	   var tableView = $find(curRg).get_masterTableView();
	   tableView.set_dataSource(result);
	   tableView.dataBind();
	   var grid = $find(curRg);
			grid.repaint();
		if(curRowIndexP61 != null)
		if(curRowIndexP61 <= curDataGridP61.length)
			curObjIdP61 = curDataGridP61[curRowIndexP61-1].Id;
		else
			curObjIdP61 = null;
	}
}
function pageLoadP61(filter)
{
	 strFilterP61 = filter;
	 if(!(strFilterP61!=null && strFilterP61.length>1))
		strFilterP61 =null ;
   var tableView = $find(otP61.rg).get_masterTableView();
    curRg = otP61.rg;
    WSMailisa_PhieuThuTienMat.GetData(0, tableView.get_pageSize(),
        tableView.get_sortExpressions().toString(), strFilterP61, 
            updateGridP61);
   WSMailisa_PhieuThuTienMat.GetCount(strFilterP61, updateVirtualItemCountP61);
   $get(otP61.lErrorCID).innerHTML = FormatDiv('','Điền thông tin vào mẫu bên dưới');
}
function RgP61_Command(sender, args)
{
    curRg = otP61.rg;
    args.set_cancel(true);
    var currentPageIndex = sender.get_masterTableView().get_currentPageIndex();
    var pageSize = sender.get_masterTableView().get_pageSize();
    var sortExpressions = sender.get_masterTableView().get_sortExpressions();
    if(sortExpressions.length > 0 && filterExpressions.length > 0 && currentPageIndex > 0)
    {
       sender.get_masterTableView().set_currentPageIndex(0);
    }
    var sortExpressionsAsSQL = sortExpressions.toString();
    WSMailisa_PhieuThuTienMat.GetData(currentPageIndex * pageSize, pageSize, sortExpressionsAsSQL,  strFilterP61, updateGridP61);
    if(args.get_commandName() == 'Filter')
    {
	 	 WSMailisa_PhieuThuTienMat.GetCount( strFilterP61, updateVirtualItemCountP61);
    }
}
function RgP61_RowDataBound(sender, args)
{
   var btnPrintCID = getChild(args, 'printP61').id;
   if(btnPrintCID ==null)
       return false;
   var btnPrint = $get(btnPrintCID);
   var Id = (args.get_dataItem()["Id"]);
   btnPrint.alt = Id;
   var btnDelCID = getChild(args, 'delP61').id;
   if(btnDelCID ==null)
       return false;
   var btnDel = $get(btnDelCID);
	 btnDel.alt = Id;
}
function changCmbNhomDoiTuongIdP61(id) 
{ 
   if(curDataDivShow)
   if(curDataDivShowIndex)
   {
	   var item = curDataDivShow[curDataDivShowIndex];
       $get(otP61.tDoiTuongIdP61CID).alt = item[2]; 
       $get(otP61.tDoiTuongIdP61CID).value = item[3]; 
   }
} 
function FocusCmbTextP61(strId, e) 
{ 
    curTCID = strId; 
    if(curTCID) 
       changeCmbTextP61(strId, e); 
} 
function changeCmbTextP61(strId, e) 
{ 
	var unicode; 
	if(window.event) 
			  unicode = window.event.keyCode;     //IE 
	else 
		  unicode = e.which;     //firefox 
	/* tab null backspace , minus*/ 
    if(unicode == 9 || unicode == 16 || unicode == 17) 
    { 
        var divShow = $get(divShowCID); 
        divShow.style.display = 'none'; 
        return true; 
     } 
    curTCID = strId; 
    if(curTCID) 
    { 
        var value = $get(curTCID).value; 
        var strFil = ' ';
        if(curTCID==otP61.tVuviecIdP61CID)
			WSMailisa_PhieuThuTienMat.get2VuviecId_Name4(value, 10, strFil, updateDiv); 
        var strFil = ' ';
        if(curTCID==otP61.tNhomDoiTuongIdP61CID)
			WSMailisa_PhieuThuTienMat.get2NhomDoiTuongId_Name5(value, 10, strFil, updateDiv); 
        var strFil = ' ';
        if(curTCID==otP61.tDoiTuongIdP61CID)
			WSMailisa_PhieuThuTienMat.get2DoiTuongId_Name6(value, 10, strFil, updateDiv); 
        var strFil = ' ';
        if(curTCID==otP61.tNhanVienIdP61CID)
			WSMailisa_PhieuThuTienMat.get2NhanVienId_HoTen11(value, 10, strFil, updateDiv); 
        var strFil = ' ';
        if(curTCID==otP61.tBranhIdP61CID)
			WSMailisa_PhieuThuTienMat.get2BranhId_Name18(value, 10, strFil, updateDiv); 
    } 
} 